package com.qf.service.impl;

import com.qf.mapper.*;
import com.qf.model.*;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.DeviceService;
import com.qf.service.ProducterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProducterServiceImpl implements ProducterService {
    @Autowired
    private ProducterMapper producterMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private FarmMapper farmMapper;
    @Autowired
    private ProducterTypeMapper producterTypeMapper;


    @Override
    public List<TProducter> findAll() {
        return producterMapper.getAllProducter();
    }


    @Override
    @Transactional
    public int add(TProducter tProducter) {
        tProducter.setCreatedate(new Date());

        return producterMapper.add(tProducter);
    }

    @Override
    public TProducter findById(int id) {
        return producterMapper.findById(id);
    }


    @Override
    @Transactional
    public int edit(TProducter tProducter) {

        int i = producterMapper.edit(tProducter);
        return i;
    }


    @Override
    @Transactional
    public int delete(int id) {
        return producterMapper.delete(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails) {
            username = ((UserDetails)principal).getUsername();
        } else {
            username = principal.toString();
        }
        System.out.println("username---------->"+username);
        int userId = sysUserMapper.findUserByUsername(username).getId();





        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        List<TProducter> rows = producterMapper.findPage((CurrentPage - 1) * PageSize, PageSize, queryString,userId);
        for (TProducter row : rows) {
            Farm farm = farmMapper.findById(row.getFarmId());
            row.setFarm(farm);
            SysUser sysUser = sysUserMapper.findById(row.getSysUserId());
            row.setSysUser(sysUser);
            ProducerType producerType = producterTypeMapper.findById(row.getProducerTypeId());
            row.setProducerType(producerType);
        }
        Long total = producterMapper.count(queryString,userId);
        pageResult.setRows(rows);
        pageResult.setTotal(total);
        return pageResult;
    }
}
